package modules.intercommunication;

import java.net.Socket;

import common.GameServer;

import modules.configuration.GameConf;

public class InterServerThreadFactory implements Runnable {

    private InterServerThreadFactory _self;

    public InterServerThreadFactory() {
        if (_self != null) {
            return;
        }
        _self = this;
        Thread mainThread = new Thread(this);
        mainThread.start();
    }

    public void run() {
        do {
            try {
                Socket s = new Socket(GameConf._interAddress, GameConf._interPort);
                new InterServerThread(s);
                break;
            } catch (Exception e) {
                GameServer.writeLine("IS Error: " + e.getMessage());
                GameServer.writeLine("Error: Inter-server socket cannot be open");
                GameServer.writeLine("Maybe the realmServer is not started...");
                GameServer.writeLine("Retry in 5 seconds");
                //Wait 5 seconds and retry
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException f) {
                };
            }
        } while (true);
        GameServer.writeLine("IS socket connected to " + GameConf._interAddress + ":" + GameConf._interPort);
    }
}
